package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TMStatRefundMapper;
import cc.rengu.igas.momp.common.entity.TMStatRefund;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 退货统计表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/27 11:08
 */
public class TMStatRefundMapperImpl implements TMStatRefundMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 获取退货统计表单条数据数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param transDate 交易日期
     * @param traceNo   前端流水号
     * @param mchntNo   商户号
     * @return cc.rengu.igas.momp.common.entity.TMStatRefund 退货统计表数据库实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/27 11:29
     */
    @Override
    public TMStatRefund selectSingleRecordFromTMStatRefund(String instId, String transDate, String traceNo, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSingleRecordFromTMStatRefund");

        TMStatRefund tmStatRefund = new TMStatRefund();
        tmStatRefund.setInstId(instId);
        tmStatRefund.setTxnDate(transDate);
        tmStatRefund.setSysSeqNum(traceNo);
        tmStatRefund.setMchntNo(mchntNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatRefund, tmStatRefund);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("查询法人机构号<{}>在<{}>的流水号<{}>的商户<{}>退货统计数据失败! RETURN_CODE=<{}>", instId, transDate, traceNo, mchntNo, iReturnCode);
            return null;
        }

        return tmStatRefund;
    }

    /**
     * 增量更新退货统计表单条数据数据库操作接口实现
     *
     * @param tmStatRefund 退货统计表数据库实体类
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/27 11:15
     */
    @Override
    public int updateSingleRecordFromTmStatRefund(TMStatRefund tmStatRefund) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSingleRecordFromTmStatRefund");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatRefund, TMStatRefund.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>在<{}>的流水号<{}>的商户<{}>的退货统计数据失败! RETURN_CODE=<{}>", tmStatRefund.getInstId(), tmStatRefund.getTxnDate(), tmStatRefund.getSysSeqNum(), tmStatRefund.getMchntNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增退货统计表单条数据数据库操作接口实现
     *
     * @param tmStatRefund 退货统计表数据库实体类
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/11 23:52
     */
    @Override
    public int insertSingleRecordFromTmStatRefund(TMStatRefund tmStatRefund) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSingleRecordFromTmStatRefund");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatRefund, TMStatRefund.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>在<{}>的流水号<{}>的商户<{}>的退货统计数据失败! RETURN_CODE=<{}>", tmStatRefund.getInstId(), tmStatRefund.getTxnDate(), tmStatRefund.getSysSeqNum(), tmStatRefund.getMchntNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 直接更新退货统计表单条数据数据库操作接口实现
     *
     * @param tmStatRefund 退货统计表数据库实体类
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/19 11:05
     */
    @Override
    public int updateSingleRecordFromTmStatRefundDirectly(TMStatRefund tmStatRefund) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSingleRecordFromTmStatRefundDirectly");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatRefund, TMStatRefund.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>在<{}>的流水号<{}>的商户<{}>的退货统计数据失败! RETURN_CODE=<{}>", tmStatRefund.getInstId(), tmStatRefund.getTxnDate(), tmStatRefund.getSysSeqNum(), tmStatRefund.getMchntNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }
}
